<template>
	<view class="main">
		<view class="cover">
			<image src="/static/rlogo.jpg" mode=""></image>
		</view>
		<!-- <button class="btn" open-type="getPhoneNumber" @getphonenumber="getphonenum">授权登录</button> -->
		<view class="btn" @click="wxLogin">授权登录</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import { login, getUserInfo } from '@/api/apiList';
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';

onShareAppMessage(() => {
	return {
		path: '/pages/login/login',
		imageUrl: '/static/rlogo.jpg',
		title: '威远人力'
	};
});

onShareTimeline(() => {
	return {
		path: '/pages/login/login',
		imageUrl: '/static/rlogo.jpg',
		title: '威远人力'
	};
});

let wxCode = ref('');
onLoad(() => {
	// uni.login({
	// 	success: (result) => {
	// 		wxCode.value = result.code;
	// 		console.log(wxCode.value);
	// 	}
	// });
});

// 登录
const wxLogin = async () => {
	uni.showLoading({
		title: '登录中',
		icon: 'none'
	});
	uni.login({
		success: async (result) => {
			wxCode.value = result.code;
			const res = await login({ code: wxCode.value });
			if (res.code == 200) {
				uni.setStorageSync('token', res.msg);
				const userRes = await getUserInfo();
				uni.hideLoading();
				uni.showToast({
					title: '登录成功',
					icon: 'none'
				});
				if (userRes.code == 200) {
					uni.setStorageSync('user', userRes.data);
					switch (userRes.data.auditStatus) {
						case 0:
							uni.reLaunch({
								url: '/pages/index/index'
							});
							break;
						case 1:
							uni.reLaunch({
								url: `/pages/index/submit?type=${userRes.data.auditStatus}`
							});
							break;
						case 3:
							uni.reLaunch({
								url: `/pages/index/submit?type=${userRes.data.auditStatus}`
							});
							break;
						case 2:
							uni.reLaunch({
								url: '/pages/index/home'
							});
							break;
						case 4:
							uni.showToast({
								title: '您已离职',
								icon: 'none'
							});
							break;
					}
				}
			}
		}
	});
};

const getphonenum = async (e) => {
	if (e.detail.errMsg == 'getPhoneNumber:ok') {
		let data = {
			phoneCode: e.detail.code,
			openId: openId.value
		};
		uni.showLoading({
			mask: true,
			title: '登录中'
		});
	}
};
</script>

<style>
page {
	height: 100vh;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background: linear-gradient(180deg, #2b77ff 0%, rgba(43, 119, 255, 0) 100%);
}
</style>

<style lang="scss" scoped>
.cover {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-bottom: 100rpx;
	image {
		width: 110rpx;
		height: 110rpx;
	}
}

.btn {
	width: 686rpx;
	height: 88rpx;
	background: #2b77ff;
	border-radius: 44rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	color: #fff;
}
</style>
